﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Business;

namespace Actuals.Business
{
    public class BusinessActualsBreakDown : IBusinessActualsBreakDown
    {

         private IBusinessActual  _businessActual;

        public BusinessActualsBreakDown(IBusinessActual businessActual)
        {
            _businessActual = businessActual;
        }

        public BusinessActualsBreakDown()
        {
            _businessActual = new BusinessActual();
        }

        public IList<IActualBreakDown> GetActualsBreakDownForDevelopers(DateTime fromDate, DateTime toDate)
        {
            return GetActualsBreakDownForDevelopers(fromDate, toDate, 0, 0);
        }

        public IList<IActualBreakDown> GetActualsBreakDownForDevelopers(DateTime fromDate, DateTime toDate, int projectId, int releaseId)
        {
           

            IList<IActual> actuals = _businessActual.GetActualsOnlyForDevelopers(fromDate, toDate,
                                                               projectId,
                                                               releaseId);
            IBusinessTask businessTask = new BusinessTask();
            IBusinessSubTask businessSubTask = new BusinessSubTask();

             var sums = actuals.ToList().GroupBy(x => new { x.TaskId, x.SubTaskId }).Select(group => new
            {
                group.Key.TaskId,
                group.Key.SubTaskId,
                TotalHours = group.Sum(x => x.NoOfHours)
            });

            IList<IActualBreakDown> actualBreakDowns = new List<IActualBreakDown>();
            foreach (var grouping in sums)
            {

                int taskId = grouping.TaskId;
                int subTaskId = grouping.SubTaskId;

                ITask task = businessTask.GetTask(taskId);
                ISubTask subTask = businessSubTask.GetSubTask(subTaskId);

                IActualBreakDown actualBreakDown = new ActualBreakDown();
                actualBreakDown.TaskDescription = task.TaskShortName;
                actualBreakDown.SubTaskId = subTaskId;
                actualBreakDown.SubTaskDescription = subTask.Description;

                actualBreakDown.TaskId = taskId;
                actualBreakDown.TotalHours = grouping.TotalHours;

                actualBreakDowns.Add(actualBreakDown);

            }

            IList<IActualBreakDown> _actualBreakDowns =
                actualBreakDowns.ToList().OrderBy(x => x.TaskId).ThenBy(x => x.SubTaskId).ToList();
            return _actualBreakDowns;

        }

        public IList<IActualBreakDown> GetActualsBreakDownForBusinessAnalysts(DateTime fromDate, DateTime toDate)
        {
            return GetActualsBreakDownForBusinessAnalysts(fromDate, toDate, 0, 0);
        }


        public IList<IActualBreakDown> GetActualsBreakDownForBusinessAnalysts(DateTime fromDate, DateTime toDate, int projectId, int releaseId)
        {


            IList<IActual> actuals = _businessActual.GetActuals(fromDate, toDate,
                                                               projectId,
                                                               releaseId);

            IList<IActual> actualsForBa = actuals.ToList().FindAll(actual => actual.EmployeeId == 6);
                
            
            

            IBusinessTask businessTask = new BusinessTask();
            IBusinessSubTask businessSubTask = new BusinessSubTask();

            var sums = actualsForBa.ToList().GroupBy(x => new { x.TaskId,x.SubTaskId }).Select(group => new
            {
                group.Key.TaskId,
                group.Key.SubTaskId,
                TotalHours = group.Sum(x => x.NoOfHours)
            });

            IList<IActualBreakDown> actualBreakDowns = new List<IActualBreakDown>();
            foreach (var grouping in sums)
            {

                int taskId = grouping.TaskId;
                int subTaskId = grouping.SubTaskId;

                ITask task = businessTask.GetTask(taskId);
                ISubTask subTask = businessSubTask.GetSubTask(subTaskId);
                IActualBreakDown actualBreakDown = new ActualBreakDown();
                actualBreakDown.TaskDescription = task.TaskShortName;
                actualBreakDown.TaskId = taskId;
                actualBreakDown.SubTaskId = subTaskId;
                actualBreakDown.SubTaskDescription = subTask.Description;


                actualBreakDown.TotalHours = grouping.TotalHours;

                actualBreakDowns.Add(actualBreakDown);

            }


            IList<IActualBreakDown> _actualBreakDowns =
               actualBreakDowns.ToList().OrderBy(x => x.TaskId).ThenBy(x => x.SubTaskId).ToList();
            return _actualBreakDowns;

        }

        public int GetTotalHours(IList<IActualBreakDown> actualBreakDowns)
        {
            return actualBreakDowns.ToList().Sum(x => x.TotalHours);
        }
    }
}
